package com.my.mynote;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SearchView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.my.adapter.MyNoteAdapter;
import com.my.bean.Note;
import com.my.helper.NoteDbOpenHelper;
import com.my.util.SpfUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private FloatingActionButton mBtnAdd;
    private List<Note> mNote;
    private MyNoteAdapter mAdapter;
    private NoteDbOpenHelper mNoteDbOpenHelper;
    private static final int MODE_LINEAR = 0;
    private static final int MODE_GRID = 1;
    //当前列表的布局格式
    private int currentListLayoutMode = MODE_LINEAR;
    private static final String KEY_LAYOUT_MODE = "key_layout_mode";

    //数据库的帮助类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //假数据
        initData();
        initEvent();
    }

    //数据库再查一次的操作
    @Override
    protected void onResume() {
        super.onResume();
        refreshDataFromDb();
        setListLayout();
    }

    private void setListLayout() {
        currentListLayoutMode = SpfUtil.getIntWithDefault(this,KEY_LAYOUT_MODE,MODE_LINEAR);
        if (currentListLayoutMode == MODE_LINEAR){
            setToLinearList();
        }else {
            setToGridList();
        }
    }
    private void setToLinearList() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
        mAdapter.setViewType(MyNoteAdapter.TYPE_LINEAR_LAYOUT);
        mAdapter.notifyDataSetChanged();
    }

    private void setToGridList() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter.setViewType(MyNoteAdapter.TYPE_GRID_LAYOUT);
        mAdapter.notifyDataSetChanged();
    }

    private void refreshDataFromDb() {
        //再查一次数据库
        mNote = getDataFromDb();
        //通知适配器
        mAdapter.refreshData(mNote);
    }

    private void initEvent() {
        mAdapter  = new MyNoteAdapter(mNote,MainActivity.this);
        mRecyclerView.setAdapter(mAdapter);
        /*//为RecyclerView设置布局管理器
        LinearLayoutManager layout = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layout);
        mAdapter.setViewType(MyNoteAdapter.TYPE_LINEAR_LAYOUT);*/
        setListLayout();
    }

    private void initData() {
        mNote = new ArrayList<>();
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);
       /* //添加多条
        for (int i = 0; i < 30; i++) {
            Note note = new Note();
            note.setTitle("这是标题"+i);
            note.setContent("这是内容"+i);
            //当前系统事件
            note.setCreate(getCurrentTimeFormat());
            mNote.add(note);
        }*/

    }

    private List<Note> getDataFromDb() {
        return mNoteDbOpenHelper.queryAllFromDb();
    }

    //获取当前系统事件的方法
    private String getCurrentTimeFormat(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.rlv);
    }

    public void add(View view) {
        Intent intent = new Intent(this,AddActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //查询
                mNote = mNoteDbOpenHelper.queryFromDbByTitle(newText);
                //刷新列表
                mAdapter.refreshData(mNote);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        item.setChecked(true);
        switch (item.getItemId()){
            case R.id.menu_linear:
                setToLinearList();
                currentListLayoutMode = MODE_LINEAR;
                //持久化保存
                SpfUtil.saveInt(this,KEY_LAYOUT_MODE,MODE_LINEAR);

                return true;
            case R.id.menu_grid:
                setToGridList();
                currentListLayoutMode = MODE_GRID;
                //持久化保存
                SpfUtil.saveInt(this,KEY_LAYOUT_MODE,MODE_GRID);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (currentListLayoutMode == MODE_LINEAR){
            MenuItem item = menu.findItem(R.id.menu_linear);
            item.setChecked(true);
        }else {
            menu.findItem(R.id.menu_grid).setChecked(true);
        }
        return super.onPrepareOptionsMenu(menu);
    }
}